package com.tydic.portal.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tydic.common.core.domain.R;
import com.tydic.common.core.exception.api.ApiException;
import com.tydic.common.mybatis.core.page.TableDataInfo;
import com.tydic.order.service.IOrderContractService;
import com.tydic.order.service.IOrderService;
import com.tydic.system.domain.ContractTemplate;
import com.tydic.system.domain.req.ContractTemplateByPageRequest;
import com.tydic.system.domain.req.ContractTemplateRequest;
import com.tydic.system.service.IContractTemplateService;
import com.tydic.system.service.IFddService;
import com.tydic.system.service.ISysOssService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 合同模板Controller
 *
 * @author zr
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/portal/contractTemplate")

public class ContractTemplateController {


    private final IContractTemplateService contractTemplateService;
    private final IOrderContractService orderContractService;
    private final IOrderService orderService;
    private final IFddService fddService;
    private ISysOssService sysOssService;

    /**
     * 添加合同模板
     *
     * @param contractTemplateRequest
     * @return
     */

    /**
     * 添加合同模板
     */
    @PostMapping("/addContactTemp")
    public R addContactTemp(@Valid ContractTemplateRequest contractTemplateRequest) {
        Boolean flg = contractTemplateService.addContractTemp(contractTemplateRequest);
        if (Boolean.TRUE.equals(flg)) {
            return R.ok("添加成功");
        } else {
            throw new ApiException("添加合同失败");
        }
    }

    /**
     * 查询合同模板列表
     *
     * @param template
     * @return
     */
    /**
     * 查询合同模板列表
     */
    @PostMapping("/queryTempList")
    public R queryTempList(ContractTemplateByPageRequest template) {
        Page<ContractTemplate> templates = contractTemplateService.queryTempList(template);
        return R.ok(TableDataInfo.build(templates));
    }

    /**
     * 启用合同模板
     *
     * @param templateCode
     * @return
     */

    /**
     * 废弃/启用合同模板
     */
    @GetMapping("/changeStatus")
    public R changeStatus(String templateCode, String status) {
        if (templateCode == null || status == null) {
            throw new ApiException("条件不符合，请重新请求");
        }
        Boolean flg = contractTemplateService.change(templateCode, status);
        return R.ok(flg);
    }

    /**
     * 更新合同模板信息
     *
     * @param request
     * @return
     */

    /**
     * 更新合同模板信息
     */
    @PostMapping("/updateTemp")
    public R updateTemp(@Valid ContractTemplateRequest request) {
        Boolean flg = contractTemplateService.updateContractTemp(request);
        return R.ok(flg);
    }

    /**
     * 查看合同模板详情
     *
     * @param id
     * @return
     */

    /**
     * 查看合同模板详情
     */
    @GetMapping("/getTemplateInfo")
    public R getTemplateInfo(String id) {
        if (id == null) {
            throw new ApiException("模板编号不能为空，请重新请求");
        }
        ContractTemplate template = contractTemplateService.getTemplateInfo(id);
        return R.ok(template);
    }

    //    TODO 合同模板填充，测试方法


}
